package cn.incast.fiction.service;

import java.util.ArrayList;

import cn.incast.fiction.domain.Fictionltem;
import cn.itcast.fiction.dao.AdminDao;
import cn.itcast.fiction.data.FictionItem;

public class AdminService {
	private AdminDao adminDao = new AdminDao();
	public ArrayList<Fictionltem> queryFictionItem(){
		ArrayList<Fictionltem> data = adminDao.queryAllData();
		return data;
	}
	public boolean addFictionItem(String number,String name, String price,String unit) {
		ArrayList<Fictionltem> data = queryFictionItem();
		for (int i = 0;i < data.size(); i++) {
			Fictionltem fictionItem = data.get(i);
			if(number.equals(fictionItem.getNumber())) {
				return false;
			}
		}
		FictionItem thisFictionItem = new FictionItem(number,name,Double.parseDouble(price),unit);
		adminDao.addFictionItem(thisFictionItem);
		return true;
	}
	public boolean updateFictionItem(String number, String name, String price,String unit) {
		ArrayList<Fictionltem> data = queryFictionItem();
		for (int i = 0; i < data.size(); i++) {
			Fictionltem fictionItem = data.get(i);
			if(number.equals(fictionItem.getNumber())) {
				adminDao.delFictionItem(number);
				FictionItem thisFictionItem = new FictionItem(number , name,Double.parseDouble(price),unit);
				adminDao.addFictionItem(thisFictionItem);
				return true;
			}
		}
		return false;
	}
	public boolean delFictionItem(String delNumber) {
		ArrayList<Fictionltem> data = queryFictionItem();
		for (int i = 0;i < data.size(); i++) {
			Fictionltem fictionItem = data.get(i);
			if(delNumber.equals(fictionItem.getNumber())) {
				adminDao.delFictionItem(delNumber);
				return true;
			}
		}
		return false;
	}
}

	